import os.path
import time
import diytool
import random
from diytool import randomStr
from diytool import uuid_maker
from send_soap_xml import post_C2
from lxml import etree
import datetime

def write_xml(path):

    parser = etree.XMLParser(encoding = "utf-8")
    tree= etree.parse(r"D://xmlsource/channel.xml",parser=parser)

    code = 'channel' + uuid_maker(25)
    phycode = 'phychannel'+ uuid_maker(22)
    pcode = 'picture' + uuid_maker(25)

    channelNode = tree.xpath('//Object[@ElementType="Channel"]')[0]
    channelNode.set("Code",code)
    channelNode.set("ID",code)

    #生成8位随机字符串
    salt = randomStr(12)
    channelname = channelNode.find('Property[@Name="Name"]')
    channelname.text = '频道'+ salt
    description = channelNode.find('Property[@Name="Description"]')
    description.text = '频道'+ salt

    phychannelNode = tree.xpath('//Object[@ElementType="PhysicalChannel"]')[0]
    phychannelNode.set("Code",phycode)
    phychannelNode.set("ID",phycode)
    channelcode = phychannelNode.find('Property[@Name="ChannelCode"]')
    channelcode.text = code;
    ChannelID = phychannelNode.find('Property[@Name="ChannelID"]')
    ChannelID.text = code;

    MultiCastPort = phychannelNode.find('Property[@Name="MultiCastPort"]')
    MultiCastPort.text = str(random.randint(8000,50000));

    picNode = tree.xpath('//Object[@ElementType="Picture"]')[0]
    picNode.set("Code",pcode)
    picNode.set("ID",pcode)

    mappingNode = tree.xpath('//Mapping[@ElementType="Channel"][@ParentType="Picture"]')[0]
    mappingNode.set("ParentCode",pcode)
    mappingNode.set("ParentID",pcode)
    mappingNode.set("ElementCode",code)
    mappingNode.set("ElementID",code)

    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')

    print("generate channel :"+ salt)

    return code

def write_schedule_xml(path, parentcode,startime):

    parser = etree.XMLParser(encoding="utf-8")

    tree = etree.parse(r"/home/projects/xmlsource/schedule.xml", parser=parser)

    objectnodes = tree.xpath('//Object[@ElementType="Schedule"]')

    for i in range(len(objectnodes)):

        code = 'schedule' + uuid_maker(24)

        scheduleObjNode = tree.xpath('//Object[@ElementType="Schedule"]')[i]
        scheduleObjNode.set("Code", code)
        scheduleObjNode.set("ID", code)

        ChannelCode = scheduleObjNode.find('Property/[@Name="ChannelCode"]')
        ChannelCode.text = parentcode

        ChannelID = scheduleObjNode.find('Property[@Name="ChannelID"]')
        ChannelID.text = parentcode
        StartDate = scheduleObjNode.find('Property[@Name="StartDate"]')
        StartDate.text = startime

    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')


count = 0
delta = -2
while (count<500):
    #request_url = "http://10.48.115.55:6080/axis/services/ctms"
    request_url = "http://10.48.115.142:8138/scms-injection/axis/services/ctms"
    csp = "55DC"
    lsp = "142DC"
    CorrelateID = diytool.timeStr()
    path ="/opt/wacos/worksheet/"
    if not os.path.exists(path):
        print("# path not exists")
        os.mkdir(path)
    #channelcode = write_xml(path + xmlfilename)
    ftpdir = "ftp://wacos:wacos@172.19.97.207:21/"
    xmlfilename = "channel" + uuid_maker(25) + ".xml"
    ftppath = ftpdir + xmlfilename
    #print(path)
    #post_C2(request_url,csp,lsp,CorrelateID,ftppath)
    #print("send channel soap request end ....")
    #sleeptime = 10
    #print("waiting for "+ str(sleeptime)+ "s ......")
    #time.sleep(sleeptime)

    channelcode='e92a760d4680478c872c7ad15a6cc1a1'
   
    #发送schedule xml

    schedule_xml_filename = "schedule"+uuid_maker(24)+".xml"
      
    startime = (datetime.datetime.now() + datetime.timedelta(days=+delta)).strftime("%Y%m%d")
      
    write_schedule_xml(path+schedule_xml_filename,channelcode,startime)
      
    scheduleftppath = ftpdir + path+ schedule_xml_filename
    # print(scheduleftppath)
      
    post_C2(request_url,csp,lsp,CorrelateID,scheduleftppath)

    delta = delta +1   
    count = count+1

    print ("send total count is : " +str(count))

    sleeptime = 60
       
    print("waiting for "+str(sleeptime) +"s ......")

    time.sleep(sleeptime)




